#!/usr/bin/env python
#coding=utf-8

import cgi
import urllib
from models import *
from google.appengine.api import users
from google.appengine.api import memcache

CACHE_TIMES = 60*60*24*7
PAGE_SIZE = 20
CATEGORIES_LIST = [ 'Business and Office',
                    'Chemicals',
                    'Comprehensive',
                    'Computers',
                    'Construction and Decorating',
                    'Consumable',
                    'Consumer Electronics',
                    'Culture and Media Production',
                    'Energy, Mining, and Minerals',
                    'Entertainment and Service',
                    'Environmental protection',
                    'Food and Beverage',
                    'Forestry and Agriculture',
                    'Gift and Artwork',
                    'Information Consultation',
                    'Machining',
                    'Maintenance',
                    'Mechanic Instruments',
                    'Medicine',
                    'Package and Printing',
                    'Real Estate',
                    'Telecommunications',
                    'Textiles and Apparel',
                    'Transportation Vehicles'
                  ]


def formatString(string):
  return unicode(str(string).strip(), "utf-8")

# {{{ format_date
def format_date(dt):
	return dt.strftime('%Y-%m-%d %H:%M')
# }}}

def getParamString(string):
  return urllib.unquote(cgi.escape(string).strip())

def reloadMemCache():
  memcache.flush_all()

  categories = []
  totalCount = 0
  for name in CATEGORIES_LIST:
    count = Buyer.getBuyerCountByCategory(name)
    categories.append(Category(name, count))
    totalCount += count
    memcache.add(name, count, CACHE_TIMES)
  
  memcache.add('categories', categories, CACHE_TIMES)
  memcache.add('totalBuyerCount', totalCount, CACHE_TIMES)

class Category:
  def __init__(self, name, count):
    self.name = name
    self.count = count

class Pager:
  def __init__(self, currentPage=1, totalCount=0, url=''):
    self.totalCount = totalCount
    self.maxPage = totalCount / PAGE_SIZE + 1
    if currentPage > self.maxPage:
      self.currentPage = self.maxPage
    else:
      self.currentPage = currentPage

    self.prevPage = self.currentPage - 1
    self.nextPage = self.currentPage + 1
    self.showPrev = not (self.currentPage <= 1)
    self.showNext = not (self.currentPage >= self.maxPage)
    
    self.url = url
    self.startPage = 1
    self.endPage = 1
    self.listPage = []
    
    if self.showPrev or self.showNext:
      if self.currentPage - 2 > 1:
        self.startPage = self.currentPage - 3
      else:
        self.startPage = 1
      if self.currentPage + 2 < self.maxPage:
        self.endPage = self.currentPage + 3
      else:
        self.endPage = self.maxPage
      self.listPage = range(self.startPage, self.endPage + 1)

  def __str__(self):
    return 'totalCount:%s,maxPage:%s,currentPage:%s,prevPage:%s,nextPage:%s,showPrev:%s,showNext:%s,startPage:%s,endPage:%s,listPage:%s' % (self.totalCount, self.maxPage, self.currentPage, self.prevPage, self.nextPage, self.showPrev, self.showNext, self.startPage, self.endPage, self.listPage)
    
class UserInfo:
  def __init__(self, user=None):
    self.user = user
    self.isLogin = False
    self.isAdmin = False
    self.loginUrl = users.create_login_url('/')
    self.logoutUrl = users.create_logout_url('/')
